from django.shortcuts import render
from django.views import View
from django.http import JsonResponse
from tools import get_ip
import base64
from apps.client import models
# Create your views here.


def home(request):
    return render(request, 'home/index.html')


class AddClient(View):

    def post(self, request):
        local_ip = get_ip()
        # local_ip = '127.0.0.1'
        ip = request.POST.get('ip')
        port = request.POST.get('port')
        name = request.POST.get('name')
        secret_str = str(ip)+','+str(port)
        secret_str_ba = base64.b64encode(secret_str.encode())
        secret = base64.b64encode(secret_str_ba +b',' + local_ip.encode()).decode()
        ip_list = models.ClientInfo.objects.filter(ip=ip)
        if ip_list:
            for i in ip_list:
                if port == str(i.port):
                    return JsonResponse({'status': 201, 'msg': '该设备已存在，禁止重复添加！'}, status=201)
        try:
            models.ClientInfo.objects.create(name=name, ip=ip,port=port, secret=secret, isactive=0)
            return JsonResponse({'status': 200, 'msg': f'添加{ip}:{port}成功！'}, status=200)
        except Exception as e:
            return JsonResponse({'status': 500, 'msg': '未知错误，请稍候重试！', 'error': str(e)}, status=500)

    def get(self, request):
        return JsonResponse({'status': 403, 'msg': '请求非法，请重试！'}, status=403)


class Register(View):

    def post(self, request):
        secret = request.POST.get('secret')
        client_ip = request.POST.get('ip')
        client_port = request.POST.get('port')
        sys_type = request.POST.get('client_sys_type', 'Windows')
        try:
            client = models.ClientInfo.objects.get(secret=secret)
            if str(client.ip) == client_ip and str(client.port) == client_port:
                if client.isactive == 1:
                    return JsonResponse({'status': 202, 'msg': f'设备已激活，不能重复激活！'}, status=202)
                client.isactive = 1
                client.sys_type = sys_type
                client.save()
                return JsonResponse({'status': 200, 'msg': f'{client.name}//{client.ip}:{client.port}, 激活成功！'},
                                    status=200)
            else:
                return JsonResponse({'status': 403, 'msg': 'ip or port 错误，请核对后重新激活！'}, status=403)
        except models.models.ObjectDoesNotExist:
            return JsonResponse({'status': 403, 'msg': '密钥secret错误，请核对后重新激活！'}, status=403)
        except Exception as e:
            print(e)
            return JsonResponse({'status': 500, 'msg': f'未知错误！，请联系管理员。--{e}'}, status=500)

    def get(self):
        return JsonResponse({'status': 403, 'msg': '请求非法，请重试！'}, status=403)


class Listen(View):
    # 监听客户端在线/离线状态
    def post(self, request):
        from django_redis import get_redis_connection
        conn = get_redis_connection()
        onlines = conn.smembers('client_secret')
        return JsonResponse({'status': 200, 'onlines': onlines}, status=200)

    def get(self, request):
        return JsonResponse({'status': 403, 'msg': '请求非法，请重试！'}, status=403)